package com.lancoo.lancooimageloader.xutils3;

import android.widget.ImageView;

import com.lancoo.lancooimageloader.LancooImageCallback;
import com.lancoo.lancooimageloader.LancooImageManager;
import com.lancoo.lancooimageloader.LancooImageOptions;
import com.lancoo.lancooimageloader.OptionConverter;

import org.xutils.image.ImageOptions;
import org.xutils.x;

/**
 * Author: Andecy
 * Time: 2017/5/23
 * Email: andecy@foxmail.com
 * Description: TODO
 */

public class xUtilsLoaderImpl implements LancooImageManager {
    @Override
    public void load(ImageView view, String url) {
        x.image().bind(view, url);
    }

    @Override
    public void load(ImageView view, String url, LancooImageOptions options) {
        OptionConverter converter = options.getConverter();
        if (converter.getImageOptions() instanceof ImageOptions) {
            x.image().bind(view, url, (ImageOptions) converter.getImageOptions());
        }
    }


    @Override
    public void load(ImageView view, String url, LancooImageCallback callback) {
        x.image().bind(view, url, new xUtilsImageCallback(callback));
    }

    @Override
    public void load(ImageView view, String url, LancooImageOptions options, LancooImageCallback callback) {
        OptionConverter converter = options.getConverter();
        if (converter.getImageOptions() instanceof ImageOptions) {
            x.image().bind(view, url, (ImageOptions) converter.getImageOptions(), new xUtilsImageCallback(callback));
        }
    }

    @Override
    public xUtilsCancelableCallback loadDrawable(String url, LancooImageOptions options, LancooImageCallback callback) {
        OptionConverter converter = options.getConverter();
        if (converter.getImageOptions() instanceof ImageOptions) {
            return new xUtilsCancelableCallback(x.image().loadDrawable(url, (ImageOptions) converter.getImageOptions(), new xUtilsImageCallback(callback)));
        }
        return null;
    }

    @Override
    public void clearMemCache() {
        x.image().clearMemCache();
    }

    //    @Deprecated
    @Override
    public void clearCacheFiles() {
        x.image().clearCacheFiles();
    }
}
